package com.lighting.smssystem.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lighting.smssystem.aspect.RequireRole;
import com.lighting.smssystem.constrants.UserRole;
import com.lighting.smssystem.model.Template;
import com.lighting.smssystem.model.User;
import com.lighting.smssystem.model.bo.TemplateList;
import com.lighting.smssystem.service.impl.TemplateService;
import com.lighting.smssystem.utils.BaseResponse;
import com.lighting.smssystem.utils.ResponseUtil;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.ServerEndpoint;
import java.util.HashMap;
import java.util.Map;

/**
 * Author LeeMaster
 *
 * 创建短信模板，新增短信模板，删除短信模板
 */
@Api(
        value = "公司信息接口",
        protocols = "http",
        description = "获取公司信息接口的API"
)
@RequestMapping("/template")
@RestController
@Slf4j
public class TemplateController {

    @Autowired
    private TemplateService templateService;

    @ApiOperation(
            value = "分页查询短信模板",
            httpMethod = "GET",
            authorizations = @Authorization(
                    "全部用户"
            )
    )
    @GetMapping("")
    @ApiResponse(code = 200,message = "",response = TemplateList.class)
    public BaseResponse queryTemplate(
            @ApiParam(name = "limit",type = "query",defaultValue = "10") @RequestParam(value = "limit",defaultValue = "10") Integer limit,
            @ApiParam(name = "offset",type = "query",defaultValue = "1") @RequestParam(value = "offset",defaultValue = "1") Integer offset
    ){
//        JSONObject data = new JSONObject();
//        JSONArray arr = new JSONArray();
//        for (int i = 0; i < 4; i++) {
//            JSONObject obj = new JSONObject();
//            obj.put("template_id", "1231" + i);
//            obj.put("template_ext_id", "asda" + i);
//            obj.put("template_name", "asda" + i);
//            obj.put("template_text", "asdasd" + i);
//            obj.put("template_order", "user_id,user_name,company_name");
//            arr.add(obj);
//        }
//        data.put("list", arr);
        log.info("查询所有的短信模版");
        Map<String, Object> data = new HashMap<>();
        data.put("list", templateService.listTemplate());
        return ResponseUtil.success(data);
    }


//    @ApiOperation(
//            value = "分页查询短信模板",
//            httpMethod = "GET",
//            authorizations = @Authorization(
//                    "ADMIN 用户"
//            )
//    )
//    @PostMapping("")
//    @RequireRole(role=UserRole.ADMIN)
//    public BaseResponse addTemplate(
//            @ApiParam @RequestBody Template template
//            ){
//
//        return null;
//    }

    @ApiOperation(
            value = "使用ID 删除短信模板",
            httpMethod = "DELETE",
            authorizations = @Authorization(
                    "ADMIN 用户"
            )
    )
    @DeleteMapping("")
    @RequireRole(role=UserRole.ADMIN)
    public BaseResponse deleteTemplate(
            @ApiParam @RequestParam(value = "id") Long id
    ){
        log.info("删除短信模版{}", id);
        return ResponseUtil.success(templateService.delTemplate(id));
    }

    @ApiOperation(
            value = "使用id修改短信",
            httpMethod = "PUT",
            authorizations = @Authorization(
                    "ADMIN 用户"
            )
    )
    @PutMapping("")
    @RequireRole(role = UserRole.ADMIN)
    public BaseResponse putTemplate(
            @ApiParam(name = "短信模版信息",type = "json") @RequestBody Template template
    ) {
        log.info("修改短息模版");
        return ResponseUtil.success(templateService.updateTemplate(template));
    }

    @ApiOperation(
            value = "添加短信模版",
            httpMethod = "POST",
            authorizations = @Authorization(
                    "ADMIN 用户"
            )
    )
    @PostMapping("")
    @RequireRole(role = UserRole.ADMIN)
    public BaseResponse addTemplate(
            @ApiParam(name = "短信模版信息", type = "json") @RequestBody Template template
    ) {
        log.info("添加短信模版");
        return ResponseUtil.success(templateService.addTemplate(template));
    }

}
